/**
 * 
 */
package feedme.dao.impl;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import org.apache.log4j.Logger;
import org.apache.log4j.chainsaw.Main;

import utils.log.LogUtil;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;

import feedme.bean.FoodBean;
import feedme.bean.FoodServerBean;
import feedme.bean.OrderBean;
import feedme.bean.OrderBean;
import feedme.bean.OrderBean;
import feedme.bean.OrderFood;
import feedme.bean.OrderUser;
import feedme.bean.UserBean;


/**
 * @author kinzhang
 *s
 */
public class MongoDBBaseDaoImpl {
    
    static private final Logger logger = Logger.getLogger(MongoDBBaseDaoImpl.class);
    
    protected MongoDBDriver dbDriver;
    
    protected String dbName;

    private String collectionName;
    
    private DB db ;
    private DBCollection collection ;
    
    /**
     * POJO -> DBObject
     * 
     * @return
     */
    public static DBObject CreateDBObject(Object obj){
        Class<?> class1 = obj.getClass();
        
        Field[] fields = class1.getDeclaredFields();
        
        DBObject dbObject = new BasicDBObject();
        
        //private Date createTime;
        //private FoodServerBean foodServer;
       // private Set<OrderFood> orderFoods = new HashSet<OrderBean.OrderFood>();
        
        for (Field field : fields) {
            Class<?> type = field.getType();
            String name = field.getName();
            
            LogUtil.instance().debug("type:" + type);
            LogUtil.instance().debug("name:" + name);
            
            try {
                Method getter = class1.getMethod("get"
                    + name.substring(0,1).toUpperCase()
                    + name.substring(1)
                    );
                Object res = getter.invoke(obj) ;
                LogUtil.instance().debug("res.getClass:" + res.getClass());
                LogUtil.instance().debug(Set.class);
                
                if (res.getClass() == Date.class) {
                    dbObject.put(name, res );
                }
                else if (type == Set.class){
                    Object[] objs = ((Set)res).toArray();
                    ArrayList arrayList = new ArrayList();
                    for (Object tmp : objs) {
                        arrayList.add(tmp);
                    }
                    dbObject.put(name, arrayList );
                }
                else{
                    LogUtil.instance().debug("ignore " + type);
                }
  
                
            } catch (SecurityException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            
           
        }
        
        return dbObject;
        
    }

    public MongoDBDriver getDbDriver() {
        return dbDriver;
    }

    public void setDbDriver(MongoDBDriver dbDriver) {
        this.dbDriver = dbDriver;
    }

    public String getDbName() {
        return dbName;
    }

    public void setDbName(String dbName) {
        this.dbName = dbName;
    }

    public String getCollectionName() {
       // logger.debug(collectionName);
        return collectionName;
    }

    public void setCollectionName(String collectionName) {
        this.collectionName = collectionName;
    }
    

    public void setCollection(DBCollection collection){
        this.collection = collection;
    }

    public DB getDb() throws UnknownHostException {
        if(db==null){
            db = dbDriver.getDB(getDbName()); 
        }
        logger.debug(db);
        return db;
    }
    public void setDb(DB db) {
        this.db = db;
    }
    
    public DBCollection getDBCollection() throws UnknownHostException{
        if(collection == null){
            collection = getDb().getCollection(getCollectionName());
        }
      //logger.debug(collection);
        return collection;
    }
    
    public static void main(String[] args){
        

        Set<OrderFood> orderFoods = new HashSet();
        FoodBean foodBean = new FoodBean();
        Set<OrderUser> orderUsers = new HashSet();
        UserBean userBean = new UserBean();
        userBean.setName("kinzhang");
        
        OrderUser user = new OrderUser();
        user.user = userBean; 
        user.number = 10;
        user = new OrderUser();
        user.user = userBean; 
        user.number = 111;
        
        orderUsers.add(user);
        
        OrderFood orderFood = new OrderFood();
        orderFood.food = foodBean;
        orderFood.orderUsers = orderUsers;
        
        orderFoods.add(orderFood);
        
        OrderBean orderBean = new OrderBean();
        orderBean.setCreateTime(new Date());
        orderBean.setOrderFoods(orderFoods);
        
        
        DBObject dbObject = CreateDBObject(orderBean);
        
        System.out.println(orderBean.toString());
        System.out.println(dbObject.toString());
    }
}
